﻿using Core.SimpleTemp.Common;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Entitys.Sys;
using Core.Toolkit.Core.Entitys;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading;
using System.Threading.Tasks;
using Core.SimpleTemp.Entitys.CBS;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace Core.SimpleTemp.Repositories.Internal
{

    /// <summary>
    /// 基于EF Core 基础仓储
    /// </summary>
    public class CoreDBContext : DbContext
    {
        IHttpContextAccessor _httpContextAccessor;
        public CoreDBContext(DbContextOptions<CoreDBContext> options, IHttpContextAccessor accessor) : base(options)
        {
            _httpContextAccessor = accessor;
        }
        public DbSet<SysDepartment> SysDepartment { get; set; }
        public DbSet<SysMenu> SysMenu { get; set; }
        public DbSet<SysRole> SysRole { get; set; }
        public DbSet<SysRoleMenu> SysRoleMenu { get; set; }
        public DbSet<SysUser> SysUser { get; set; }
        public DbSet<SysUserRole> SysUserRole { get; set; }
        public DbSet<SysLoginLog> SysLoginLog { get; set; }

        public DbSet<SysDataDic> SysDataDic { get; set; }

        public DbSet<SysFile> SysFile { get; set; }

        public DbSet<SysUserInfor> SysUserInfor { get; set; }
        public DbSet<SysFactory> SysFactory { get; set; }
        public DbSet<SysDuty> SysDuty { get; set; }
        public DbSet<SysDepartmentDuty> SysDepartmentDuty { get; set; }
        public DbSet<SysUserDuty> SysUserDuty { get; set; }
        /// <summary>
        ///基础_特殊日期
        /// </summary>
        public DbSet<SysHoliday> SysHoliday { get; set; }
        public DbSet<SysMessage> SysMessage { get; set; }
        public DbSet<SysUserDepartmentDuty> SysUserDepartmentDuty { get; set; }
        /// <summary>
        /// 基础_作业地点（位置）
        /// </summary>
        public DbSet<SysJobPlace> SysJobPlace { get; set; }
        

        #region 法律法规
        /// <summary>
        /// 法律法规_标准库
        /// </summary>
        public DbSet<FL_StandardInformation> FL_StandardInformation { get; set; }
        public DbSet<FL_StandardInformationHis> FL_StandardInformationHis { get; set; }
        /// <summary>
        /// 法律法规_标准库 部门关系
        /// </summary>

        public DbSet<FL_StandardInformationDept> FL_StandardInformationDept { get; set; }
        public DbSet<FL_StandardInformationHisDept> FL_StandardInformationHisDept { get; set; }
        /// <summary>
        /// 法律法规_文件级别
        /// </summary>
        public DbSet<FL_FileLevel> FL_FileLevel { get; set; }
        /// <summary>
        /// 法律法规_合规性评价
        /// </summary>
        public DbSet<FL_Evaluation> FL_Evaluation { get; set; }
        /// <summary>
        ///法律法规_标准库符合性评审
        /// </summary>
        public DbSet<FL_StandardInformationReview> FL_StandardInformationReview { get; set; }

        /// <summary>
        /// 管理制度
        /// </summary>
        public DbSet<FL_System> FL_System { get; set; }
        /// <summary>
        /// 管理制度_相关部门
        /// </summary>
        public DbSet<FL_SystemDept> FL_SystemDept { get; set; }
        /// <summary>
        /// 管理制度_文件级别
        /// </summary>
        public DbSet<FL_SystemFileLevel> FL_SystemFileLevel { get; set; }

        /// <summary>
        /// 法律法规_合规性评价不符合项
        /// </summary>
        public DbSet<FL_EvaluationNotConform> FL_EvaluationNotConform { get; set; }

        /// <summary>
        /// 法律法规_基础库
        /// </summary>
        public DbSet<FL_BasisInformation> FL_BasisInformation { get; set; }

        /// <summary>
        /// 法律法规_基础库审核
        /// </summary>
        public DbSet<FL_BasisInformationCheck> FL_BasisInformationCheck { get; set; }

        #endregion


        #region 风险
        /// <summary>
        /// 风险_操作及相关作业活动
        /// </summary>
        public DbSet<FX_OperationAndWork> FX_OperationAndWorks { get; set; }
        /// <summary>
        /// 基础_风险等级
        /// </summary>
        public DbSet<FX_Grade> FX_Grades { get; set; }
        /// <summary>
        /// 风险_操作程序
        /// </summary>
        public DbSet<FX_Operation> FX_Operation { get; set; }
        /// <summary>
        /// 风险_作业活动
        /// </summary>
        public DbSet<FX_WorkActivity> FX_WorkActivity { get; set; }
        /// <summary>
        /// 风险_检维修作业活动
        /// </summary>
        public DbSet<FX_CheckRepair> FX_CheckRepair { get; set; }

        /// <summary>
        /// 风险_检维修作业活动工具
        /// </summary>
        public DbSet<FX_CheckRepairTool> FX_CheckRepairTool { get; set; }

        /// <summary>
        /// 风险_设备设施
        /// </summary>
        public DbSet<FX_Equipment> FX_Equipment { get; set; }
        /// <summary>
        /// 风险_工艺介质
        /// </summary>
        public DbSet<FX_Process> FX_Process { get; set; }
        /// <summary>
        /// 风险_场所环境
        /// </summary>
        public DbSet<FX_Environment> FX_Environment { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DbSet<FX_FXType> FX_FXType { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DbSet<FX_MapPosition> FX_MapPosition { get; set; }
        #endregion

        #region 承包商模块
        /// <summary>
        /// 承包商
        /// </summary>
        public DbSet<CBS_List> CBS_List { get; set; }
        /// <summary>
        /// 承包商附件
        /// </summary>
        public DbSet<CBS_ListAttachment> CBS_ListAttachment { get; set; }
        /// <summary>
        /// 承包商_人员名录
        /// </summary>
        public DbSet<CBS_User> CBS_User { get; set; }

        /// <summary>
        /// 承包商人员附件
        /// </summary>
        public DbSet<CBS_UserAttachment> CBS_UserAttachment { get; set; }
        /// <summary>
        /// 承包商_入场人员
        /// </summary>
        public DbSet<CBS_IntoFactoryUser> CBS_IntoFactoryUser { get; set; }

        /// <summary>
        /// 承包商_入场工具
        /// </summary>
        public DbSet<CBS_IntoFactoryTool> CBS_IntoFactoryTool { get; set; }

        /// <summary>
        /// 承包商_出厂工具
        /// </summary>
        public DbSet<CBS_OutFactoryTool> CBS_OutFactoryTool { get; set; }

        /// <summary>
        /// 承包商_培训课件
        /// </summary>
        public DbSet<CBS_Course> CBS_Course { get; set; }
        
        /// <summary>
        /// //承包商_培训记录
        /// </summary>
        public DbSet<CBS_TrainRecord> CBS_TrainRecord { get; set; }

        /// <summary>
        /// //承包商_培训记录_人员关系表
        /// </summary>
        public DbSet<CBS_TrainRecordUser> CBS_TrainRecordUser { get; set; }

        /// <summary>
        /// 承包商_考试记录
        /// </summary>
        public DbSet<CBS_ExamRecord> CBS_ExamRecord { get; set; }

        /// <summary>
        /// 承包商_违章记录
        /// </summary>
        public DbSet<CBS_BreakRules> CBS_BreakRules { get; set; }
        /// <summary>
        /// 承包商_违章记录_附件(0违章1图片)
        /// </summary>
        public DbSet<CBS_BreakRulesAttachment> CBS_BreakRulesAttachment { get; set; }

        public DbSet<CBS_BreakRulesApprove> CBS_BreakRulesApprove { get; set; }


        /// <summary>
        /// 承包商_日常评估检查项
        /// </summary>
        public DbSet<CBS_DayCheckItem> CBS_DayCheckItem { get; set; }

        /// <summary>
        /// 承包商_日常评估考核任务
        /// </summary>
        public DbSet<CBS_DayCheckTask> CBS_DayCheckTask { get; set; }
        /// <summary>
        /// 承包商_日常评估考核
        /// </summary>
        public DbSet<CBS_DayCheck> CBS_DayCheck { get; set; }
        /// <summary>
        /// 承包商_日常评估考核明细
        /// </summary>
        public DbSet<CBS_DayCheckDetail> CBS_DayCheckDetail { get; set; }


        /// <summary>
        /// 承包商_总体评估检查项
        /// </summary>
        public DbSet<CBS_TotalCheckItem> CBS_TotalCheckItem { get; set; }

        /// <summary>
        /// 承包商_总体评估考核任务
        /// </summary>
        public DbSet<CBS_TotalCheckTask> CBS_TotalCheckTask { get; set; }
        /// <summary>
        /// 承包商_总体评估考核
        /// </summary>
        public DbSet<CBS_TotalCheck> CBS_TotalCheck { get; set; }
        /// <summary>
        /// 承包商_总体评估考核明细
        /// </summary>
        public DbSet<CBS_TotalCheckDetail> CBS_TotalCheckDetail { get; set; }
        /// <summary>
        /// 承包商_承包商类别同主管部门
        /// </summary>
        public DbSet<CBS_CategoryManage> CBS_CategoryManage { get; set; }
        /// <summary>
        /// 承包商_承包商类别审批领导
        /// </summary>
        public DbSet<CBS_CategoryApproveDuty> CBS_CategoryApproveDuty { get; set; }
        /// <summary>
        /// 承包商_总体评估考核领导签字
        /// </summary>
        public DbSet<CBS_TotalCheckHeadApprove> CBS_TotalCheckHeadApprove { get; set; }

        /// <summary>
        /// 承包商_课件分类
        /// </summary>
        public DbSet<CBS_CourseClass> CBS_CourseClass { get; set; }

        /// <summary>
        /// 承包商_试题选项
        /// </summary>
        public DbSet<CBS_QuestionsOptions> CBS_QuestionsOptions { get; set; }
        /// <summary>
        /// 承包商_试题
        /// </summary>
        public DbSet<CBS_Questions> CBS_Questions { get; set; }
        /// <summary>
        /// 承包商_考试试题答题人
        /// </summary>
        public DbSet<CBS_ExamQuestionsUser> CBS_ExamQuestionsUser { get; set; }
        /// <summary>
        /// 承包商_考试试题明细表
        /// </summary>
        public DbSet<CBS_ExamQuestionsDetail> CBS_ExamQuestionsDetail { get; set; }
        /// <summary>
        /// 承包商_考试试题主表
        /// </summary>
        public DbSet<CBS_ExamQuestions> CBS_ExamQuestions { get; set; }
        #region 教育
        /// <summary>
        /// 我的培训
        /// </summary>
        public DbSet<PX_Plan> PX_Plan { get; set; }
        /// <summary>
        /// 培训人
        /// </summary>
        public DbSet<PX_Certificate> PX_Certificate { get; set; }
        public DbSet<PX_CertificateAttachment> PX_CertificateAttachment { get; set; }
        public DbSet<PX_PlanUser> PX_PlanUser { get; set; }
        public DbSet<PX_CourseTeacher> PX_CourseTeacher { get; set; }
        public DbSet<PX_ExamRecord> PX_ExamRecord { get; set; }
        public DbSet<PX_ExamUser> PX_ExamUser { get; set; }
        public DbSet<PX_ExamUserQuestions> PX_ExamUserQuestions { get; set; }
        public DbSet<PX_Matrix> PX_Matrix { get; set; }
        public DbSet<PX_OperationCheck> PX_OperationCheck { get; set; }
        public DbSet<PX_PlanReport> PX_PlanReport { get; set; }
        public DbSet<PX_Questions> PX_Questions { get; set; }
        public DbSet<PX_QuestionsOptions> PX_QuestionsOptions { get; set; }

        public DbSet<PX_ReportCard> PX_ReportCard { get; set; }

        public DbSet<PX_ReportCardDetail> PX_ReportCardDetail { get; set; }

        public DbSet<PX_Sign> PX_Sign { get; set; }
        public DbSet<PX_SignDetail> PX_SignDetail { get; set; }
        /// <summary>
        /// 培训课件
        /// </summary>
        public DbSet<PX_Course> PX_Course { get; set; }
        /// <summary>
        /// 教育—— 考试
        /// </summary>
        public DbSet<PX_Exam> PX_Exam { get; set; }
        public DbSet<PX_CourseClass> PX_CourseClass { get; set; }

        public DbSet<PX_Teacher> PX_Teacher { get; set; }

        #endregion

        #region 隐患
        /// <summary>
        /// 基础_隐患等级
        /// </summary>
        public DbSet<YH_Grade> YH_Grade { get; set; }
        /// <summary>
        /// 基础_隐患类型
        /// </summary>
        public DbSet<YH_Type> YH_Type { get; set; }

        /// <summary>
        /// 基础_隐患等级通知岗位
        /// </summary>
        public DbSet<YH_GradePost> YH_GradePost { get; set; }
        /// <summary>
        /// 隐患_隐患排查类别
        /// </summary>
        public DbSet<YH_Category> YH_Category { get; set; }
        /// <summary>
        /// 隐患_基础管理类隐患类型
        /// </summary>
        public DbSet<YH_BasisType> YH_BasisType { get; set; }
        /// <summary>
        /// 隐患_隐患台账
        /// </summary>
        public DbSet<YH_StandingBook> YH_StandingBook { get; set; }
        /// <summary>
        /// 隐患_隐患排查
        /// </summary>
        public DbSet<YH_Screening> YH_Screening { get; set; }
        /// <summary>
        /// 隐患_隐患排查参与人
        /// </summary>
        public DbSet<YH_ScreeningUser> YH_ScreeningUser { get; set; }
        /// <summary>
        /// 隐患_隐患排查项目
        /// </summary>
        public DbSet<YH_ScreeningProject> YH_ScreeningProject { get; set; }
        /// <summary>
        /// 隐患_隐患排查明细
        /// </summary>
        public DbSet<YH_ScreeningDetail> YH_ScreeningDetail { get; set; }
        /// <summary>
        /// 隐患排查签字
        /// </summary>
        public DbSet<YH_DangerSignature> YH_DangerSignature { get; set; }
        /// <summary>
        /// 隐患_隐患台账_附件
        /// </summary>
        public DbSet<YH_StandingBookAttachment> YH_StandingBookAttachment { get; set; }
        /// <summary>
        /// 隐患_隐患台账_流转记录
        /// </summary>
        public DbSet<YH_StandingBookProcess> YH_StandingBookProcess { get; set; }
        /// <summary>
        /// 隐患_排查计划主表
        /// </summary>
        public DbSet<YH_ScreeningPlan> YH_ScreeningPlan { get; set; }
        /// <summary>
        /// 隐患_排查计划项目
        /// </summary>
        public DbSet<YH_ScreeningPlanProject> YH_ScreeningPlanProject { get; set; }
        /// <summary>
        /// 隐患_排查计划参与人员
        /// </summary>
        public DbSet<YH_ScreeningPlanUser> YH_ScreeningPlanUser { get; set; }
        /// <summary>
        /// 隐患_项目详细
        /// </summary>
        public DbSet<YH_ScreeningPlanProDetail> YH_ScreeningPlanProDetail { get; set; }
        /// <summary>
        /// 隐患_提醒表
        /// </summary>
        public DbSet<YH_ScreeningPlanTask> YH_ScreeningPlanTask { get; set; }
        /// <summary>
        /// 隐患_提醒人表
        /// </summary>
        public DbSet<YH_ScreeningPlanWarn> YH_ScreeningPlanWarn { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DbSet<YH_BasisInformation> YH_BasisInformation { get; set; }
        /// <summary>
        /// 隐患_STOP观察记录
        /// </summary>
        public DbSet<JG_STOPObserveMain> JG_STOPObserveMain { get; set; }
        /// <summary>
        /// 隐患_STOP观察记录明细表
        /// </summary>
        public DbSet<JG_STOPObserveDetail> JG_STOPObserveDetail { get; set; }

        public DbSet<JG_STOPTimes> JG_STOPTimes { get; set; }

        public DbSet<SysArea> SysArea { get; set; }
        public DbSet<SysAreaPoint> SysAreaPoint { get; set; }
        #endregion

        #endregion

        #region 隐患模块
        //操作与相关作业活动列表
        public DbSet<YH_OperationAndWork> YH_OperationAndWork { get; set; }
        //隐患_检维修作业活动
        public DbSet<YH_CheckRepair> YH_CheckRepair { get; set; }
        //隐患_设备设施
        public DbSet<YH_Equipment> YH_Equipment { get; set; }
        // 隐患_工艺介质
        public DbSet<YH_Process> YH_Process { get; set; }
        /// <summary>
        /// 隐患_场所环境
        /// </summary>
        public DbSet<YH_Environment> YH_Environment { get; set; }


        #endregion



        #region 机构与职责
        //个人安全行动计划管理
        public DbSet<JG_PersonalSafety> JG_PersonalSafety { get; set; }
        public DbSet<JG_PersonalSafetyDetail> JG_PersonalSafetyDetail { get; set; }
        //HSE行动计划跟进表
        public DbSet<JG_HSEPlan> JG_HSEPlan { get; set; }
        //岗位安全考核模板设置列表
        public DbSet<JG_JobsAssessTemplate> JG_JobsAssessTemplate { get; set; }
        public DbSet<JG_JobsAssessTemplateDetail> JG_JobsAssessTemplateDetail { get; set; }
        // 岗位安全职责考核管理列表
        public DbSet<JG_JobsAssessTask> JG_JobsAssessTask { get; set; }
        public DbSet<JG_JobsAssess> JG_JobsAssess { get; set; }

        public DbSet<JG_JobsAssessDetail> JG_JobsAssessDetail { get; set; }

        public DbSet<JG_DutiesTemplate> JG_DutiesTemplate { get; set; }

        public DbSet<JG_DutiesPlan> JG_DutiesPlan { get; set; }
        public DbSet<JG_DutiesPlanAssessedPerson> JG_DutiesPlanAssessedPerson { get; set; }
        #endregion


        #region 总部

        //总部_考试人员
        public DbSet<ZB_ExamUser> ZB_ExamUser { get; set; }

        //总部_考试记录
        public DbSet<ZB_ExamRecord> ZB_ExamRecord { get; set; }

        //总部_考试试题
        public DbSet<ZB_ExamQuestions> ZB_ExamQuestions { get; set; }


        //总部_人员年学习分数表
        public DbSet<ZB_YearStudyNum> ZB_YearStudyNum { get; set; }

        //总部-安全检查
        public DbSet<ZB_SafetyCheck> ZB_SafetyCheck { get; set; }

        //总部-课件
        public DbSet<ZB_Course> ZB_Course { get; set; }

        //总部-课件试题
        public DbSet<ZB_CourseQuestions> ZB_CourseQuestions { get; set; }

        //总部-课件试题选项
        public DbSet<ZB_QuestionsOptions> ZB_QuestionsOptions { get; set; }

        //总部-年度培训计划
        public DbSet<ZB_TrainPlan> ZB_TrainPlan { get; set; }

        //总部-年度培训计划考题
        public DbSet<ZB_TrainPlanQuestions> ZB_TrainPlanQuestions { get; set; }

        //总部-安委会会议纪要
        public DbSet<ZB_SafetyMeeting> ZB_SafetyMeeting { get; set; }
      
        //总部_安全里程碑活动
        public DbSet<ZB_SafetyMilestone> ZB_SafetyMilestone { get; set; }

        //总部_安全里程碑活动--附件表
        public DbSet<ZB_SafetyMilestoneFile> ZB_SafetyMilestoneFile { get; set; }

        //总部_安全分委会架构
        public DbSet<ZB_SafetyFrameWork> ZB_SafetyFrameWork { get; set; }

        //总部_安全分委会成员
        public DbSet<ZB_SafetyUser> ZB_SafetyUser { get; set; }

        //总部_办公安全
        public DbSet<ZB_OfficeSafety> ZB_OfficeSafety { get; set; }

        //总部_项目类型——字典表
        public DbSet<ZB_ProjectTypes> ZB_ProjectTypes { get; set; }




        //总部_工厂运营类HSE证书库
        public DbSet<ZB_HSECertificate> ZB_HSECertificate { get; set; }
        //总部_安全管理培训师
        public DbSet<ZB_SafetyTrainer> ZB_SafetyTrainer { get; set; }
        //总部_安全管理培训师明细
        public DbSet<ZB_SafetyTrainerDetail> ZB_SafetyTrainerDetail { get; set; }

        //总部-安全经验分享
        public DbSet<ZB_SafetyShare> ZB_SafetyShare { get; set; }
        //总部-MSDS(Material Safety Data Sheet)
        public DbSet<ZB_MSDS> ZB_MSDS { get; set; }
        #endregion

        #region 作业
        /// <summary>
        /// 基础_作业字典
        /// </summary>
        public DbSet<ZY_Dict> ZY_Dict { get; set; }
        /// <summary>
        /// 基础_作业表格审批流程设置
        /// </summary>
        public DbSet<ZY_TableApproveProcess> ZY_TableApproveProcess { get; set; }
        /// <summary>
        /// 作业_作业许可证
        /// </summary>
        public DbSet<ZY_license> ZY_license { get; set; }
        /// <summary>
        /// 作业_作业内容
        /// </summary>
        public DbSet<ZY_Content> ZY_Content { get; set; }
        /// <summary>
        /// 作业票编号
        /// </summary>
        public DbSet<ZY_Number> ZY_Number { get; set; }
        /// <summary>
        /// 作业_审批签字
        /// </summary>
        public DbSet<ZY_CheckSignature> ZY_CheckSignature { get; set; }
        /// <summary>
        /// 作业_JSA分析
        /// </summary>
        public DbSet<ZY_JSA> ZY_JSA { get; set; }
        /// <summary>
        /// 作业_安全交底
        /// </summary>
        public DbSet<ZY_SafetyCard> ZY_SafetyCard { get; set; }
        public DbSet<ZY_SafetyEducation> ZY_SafetyEducation { get; set; }
        /// <summary>
        /// 作业_确认签字
        /// </summary>
        public DbSet<ZY_ConfirmSignature> ZY_ConfirmSignature { get; set; }
        

        #endregion

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {


            modelBuilder.Entity<SysDataDic>();
            //UserRole组合主键
            var v = modelBuilder.Entity<SysUserRole>();

            var v2 = modelBuilder.Entity<SysUser>();
            v2.RemoeForeignKey("SysDepartmentId", "SysUser");
            v2.HasOne(u => u.SysUserInfor).WithOne().HasForeignKey<SysUserInfor>(i => i.SysUserID);

            var v3 = modelBuilder.Entity<SysUserRole>();
            v.HasKey(ur => new { ur.SysUserId, ur.SysRoleId });
            v3.RemoeForeignKey("SysUserId");
            v3.RemoeForeignKey("SysRoleId");
            //RoleMenu组合主键
            modelBuilder.Entity<SysRoleMenu>()
              .HasKey(rm => new { rm.SysRoleId, rm.SysMenuId });


            //法律法规_标准库相关部门->法律法规_标准库
            var v8 = modelBuilder.Entity<FL_StandardInformation>();
            v8.HasMany(u => u.FL_StandardInformations).WithOne().HasForeignKey(i => i.LawRegulationsName);

            //法律法规_文件级别_FL_StandardInformation与FL_FileLevel关系
            var fxaaa = modelBuilder.Entity<FL_StandardInformation>();
            fxaaa.HasOne(u => u.FL_FileLevel).WithMany().HasForeignKey(i => i.FileLeve);

            //法律法规_用户角色_FL_StandardInformation与SysUser关系
            var fxbbb = modelBuilder.Entity<FL_StandardInformation>();
            fxbbb.HasOne(u => u.SysUser).WithOne().HasForeignKey<FL_StandardInformation>(i => i.InputPerson);


            //法律法规--历史_标准库相关部门->法律法规_标准库--历史
            var v8His = modelBuilder.Entity<FL_StandardInformationHis>();
            v8His.HasMany(u => u.FL_StandardInformations).WithOne().HasForeignKey(i => i.StandardInformationHisId);

            //法律法规--历史_文件级别_FL_StandardInformation与FL_FileLevel关系
            var fxaaaHis = modelBuilder.Entity<FL_StandardInformationHis>();
            fxaaaHis.HasOne(u => u.FL_FileLevel).WithOne().HasForeignKey<FL_StandardInformationHis>(i => i.FileLeve);

            //法律法规_用户角色_FL_StandardInformation与SysUser关系
            var fxbbbHis = modelBuilder.Entity<FL_StandardInformationHis>();
            fxbbbHis.HasOne(u => u.SysUser).WithOne().HasForeignKey<FL_StandardInformationHis>(i => i.InputPerson);

            //法律法规_文件级别_FL_FileLevel 与 管理制度 FL_System 的关系
            var fxccc = modelBuilder.Entity<FL_System>();
            fxccc.HasOne(u => u.FL_SystemFileLevel).WithOne().HasForeignKey<FL_System>(i => i.FileLeve);

            //管理制度_文件级别_FL_SystemFileLevel 与 人员 SysUser 的关系
            var fxddd = modelBuilder.Entity<FL_SystemFileLevel>();
            fxddd.HasOne(u => u.SysUser).WithOne().HasForeignKey<FL_SystemFileLevel>(i => i.CreateUser);
            //法律法规_合规性评价_FL_Evaluation 与 部门 SysDepartment 的关系
            var fxeee = modelBuilder.Entity<FL_Evaluation>();
            fxeee.HasOne(u => u.SysDepartment).WithOne().HasForeignKey<FL_Evaluation>(i => i.CreateDept);

            //法律法规_合规性评价_FL_Evaluation 与 人员 SysUser 的关系--审核人
            var fxfff = modelBuilder.Entity<FL_Evaluation>();
            fxfff.HasOne(u => u.SysUser).WithOne().HasForeignKey<FL_Evaluation>(i => i.auditor);
            //法律法规_合规性评价_FL_Evaluation 与 人员 SysUser 的关系--批准人
            var fxggg = modelBuilder.Entity<FL_Evaluation>();
            fxggg.HasOne(u => u.SysUser2).WithOne().HasForeignKey<FL_Evaluation>(i => i.approver);

            //法律法规 FL_StandardInformation与法律法规_标准库符合性评审 FL_StandardInformationReview关系
            var fxhhh = modelBuilder.Entity<FL_StandardInformation>();
            fxhhh.HasMany(u => u.FL_StandardInformationReview).WithOne().HasForeignKey(i => i.StandardInformationID);

            //管理制度_文件级别_FL_FileLevel 与 人员 SysUser 的关系
            var fxiii = modelBuilder.Entity<FL_FileLevel>();
            fxiii.HasOne(u => u.SysUser).WithOne().HasForeignKey<FL_FileLevel>(i => i.CreateUser);

            //法律法规基础库 _FL_BasisInformation与 文件级别 FL_FileLevel关系
            var fxjjj = modelBuilder.Entity<FL_BasisInformation>();
            fxjjj.HasOne(u => u.FL_FileLevel).WithOne().HasForeignKey<FL_BasisInformation>(i => i.FileLeve);

            //法律法规基础库 _FL_BasisInformation与 用户 SysUser关系
            var fxkkk = modelBuilder.Entity<FL_BasisInformation>();
            fxkkk.HasOne(u => u.SysUser).WithOne().HasForeignKey<FL_BasisInformation>(i => i.CreateUser);

            var v9k = modelBuilder.Entity<FL_BasisInformation>();
            v9k.HasOne(u => u.FL_BasisInformationCheck).WithOne().HasForeignKey<FL_BasisInformationCheck>(i => i.BasisInformationID);




            //培训记录->培训人
            var v9 = modelBuilder.Entity<CBS_TrainRecord>();
            v9.HasMany(u => u.CBS_TrainRecordUser).WithOne().HasForeignKey(i => i.CBSTrainRecordId);
            //承包商
            v9.HasOne(u => u.CBS_List).WithMany().HasForeignKey(i => i.Trainer).HasPrincipalKey(u => u.Id);
            //培训记录->培训课件
            v9.HasOne(u => u.CBS_List).WithMany().HasForeignKey(i => i.CBSListID);
            v9.HasOne(u => u.CBS_Course).WithMany().HasForeignKey(i => i.CBSCourseId);
            v9.HasOne(u => u.TrainerModel).WithMany().HasForeignKey(i => i.Trainer).HasPrincipalKey(u => u.SysUserID);
            v9.RemoeForeignKey("Trainer");
            //移除培训记录对培训课件的外键
            v9.RemoeForeignKey("CBSCourseId");
            //培训<>培训人关系表 
            var v10 = modelBuilder.Entity<CBS_TrainRecordUser>();
            //培训<>培训人关系表 ->培训人关系设置
            v10.HasOne(tu => tu.CBS_User).WithMany().HasForeignKey(i => i.UserId);
            //移除培训人对培训记录外键
            v10.RemoeForeignKey("CBSTrainRecordId");
            //移除培训人对培训人
            v10.RemoeForeignKey("UserId");


            //考试记录
            var v12 = modelBuilder.Entity<CBS_ExamRecord>();
            //考试记录-> 培训课件
            //v12.HasOne(u => u.CBS_Course).WithMany().HasForeignKey(i => i.CBSCourseId);//
            //考试记录-> 供应商
            v12.HasOne(u => u.CBS_List).WithMany().HasForeignKey(i => i.CBSListId);
            //考试记录-> 考核人
            v12.HasOne(u => u.CBS_User).WithMany().HasForeignKey(i => i.Examiner);
            //培训记录
            v12.HasOne(u => u.CBS_TrainRecord).WithMany().HasForeignKey(i => i.TrainRecordId);
            //考试试题
            v12.HasOne(u => u.CBS_ExamQuestions).WithMany().HasForeignKey(i => i.CBSExamQuestionsId);

            #region 教育
            ////培训记录
            //var v13 = modelBuilder.Entity<PX_Plan>();
            ////培训记录-培训人
            //v13.HasMany(c => c.PX_PlanUser).WithOne().HasForeignKey(c => c.PlanID);
            ////培训记录-考试人
            //v13.HasMany(c => c.PX_ExamUser).WithOne().HasForeignKey(c => c.PX_PlanId);

            ////培训课件
            //var v14 = modelBuilder.Entity<PX_Course>();
            ////培训课件-培训老师
            //v14.HasOne(c => c.PX_CourseTeacher).WithOne().HasForeignKey<PX_CourseTeacher>(c => c.trainingcourse);
            ////培训课件-课件类别
            //v14.HasOne(c => c.PX_CourseClass).WithMany().HasForeignKey(c => c.ClassificationofCourseware);

            ////培训_考试信息
            //var v15 = modelBuilder.Entity<PX_Exam>();
            ////考试信息->考题
            //v15.HasMany(c => c.PX_Questions).WithOne().HasForeignKey(c => c.trainingcourse);
            ////考试->培训
            //v15.HasOne(c => c.PX_Plan).WithMany().HasForeignKey(a => a.PlanId);
            //v15.HasMany(c => c.PX_ExamUser).WithOne().HasForeignKey(c => c.ExamId);
            //var v16 = modelBuilder.Entity<PX_Questions>();
            ////考题->考试信息
            //v16.HasMany(c => c.PX_QuestionsOptions).WithOne().HasForeignKey(c => c.QuestionsId);

            //培训记录
            var v13 = modelBuilder.Entity<PX_Plan>();
            //培训记录-培训人
            v13.HasMany(c => c.PX_PlanUser).WithOne().HasForeignKey(c => c.PlanID);
            //培训记录-考试人
            v13.HasMany(c => c.PX_ExamUser).WithOne().HasForeignKey(c => c.PX_PlanId);
            //附件
            v13.HasMany(c => c.PX_SignDetail).WithOne().HasForeignKey(c => c.SignID);
            //考试信息
            v13.HasOne(u => u.PX_Exam).WithOne().HasForeignKey<PX_Exam>(i => i.PlanId);

            v13.HasOne(c => c.PX_Course).WithMany().HasForeignKey(i => i.trainingcourse);
            //总结报告
            v13.HasOne(u => u.PX_PlanReport).WithOne().HasForeignKey<PX_PlanReport>(i => i.PlanId);
            //签到
            v13.HasOne(u => u.PX_Sign).WithOne().HasForeignKey<PX_Sign>(i => i.PlanID);
            //成绩单
            v13.HasOne(u => u.PX_ReportCard).WithOne().HasForeignKey<PX_ReportCard>(i => i.PlanID);

            //培训课件
            var v14 = modelBuilder.Entity<PX_Course>();
            //培训课件-培训老师
            v14.HasOne(c => c.PX_CourseTeacher).WithOne().HasForeignKey<PX_CourseTeacher>(c => c.trainingcourse);
            //培训课件-课件类别
            v14.HasOne(c => c.PX_CourseClass).WithMany().HasForeignKey(c => c.ClassificationofCourseware);
            //课件html地址
            v14.HasOne(c => c.SysFile).WithOne().HasForeignKey<PX_Course>(i => i.CourseAttachment);

            //培训_考试信息
            var v15 = modelBuilder.Entity<PX_Exam>();
            //考试信息->考题
            v15.HasMany(c => c.PX_Questions).WithOne().HasForeignKey(c => c.trainingcourse);
            //考试->培训
            v15.HasOne(c => c.PX_Plan).WithMany().HasForeignKey(a => a.PlanId);
            v15.HasMany(c => c.PX_ExamUser).WithOne().HasForeignKey(c => c.ExamId);


            var v16 = modelBuilder.Entity<PX_Questions>();
            //试题-选项
            v16.HasMany(c => c.PX_QuestionsOptions).WithOne().HasForeignKey(c => c.QuestionsId);

            //考试记录
            var examperson_record = modelBuilder.Entity<PX_ExamUser>();
            examperson_record.HasMany(u => u.PX_ExamRecord).WithOne().HasForeignKey(a => a.PersonID);

            //考试记录试题

            var exampersonrecord_question = modelBuilder.Entity<PX_ExamRecord>();
            exampersonrecord_question.HasMany(u => u.PX_ExamUserQuestions).WithOne().HasForeignKey(a => a.PersonID);

            #endregion


            #region 隐患
            //隐患_隐患台账
            var yhv1 = modelBuilder.Entity<YH_StandingBook>();
            //隐患_隐患台账->附件 一对多
            yhv1.HasMany(u => u.YH_StandingBookAttachment).WithOne().HasForeignKey(i => i.StandingBookId);
            //隐患-台账-流转记录 
            yhv1.HasMany(u => u.YH_StandingBookProcess).WithOne().HasForeignKey(i => i.StandingBookId);

            //隐患_隐患台账->隐患_隐患排查明细 一对多
            //台账和隐患排查明细是1：1 而且台账是排查明细的子表
            //yhv1.HasMany(u => u.YH_ScreeningDetail).WithOne().HasForeignKey(i => i.StandingBookId);


            //隐患_排查计划主表 -> 隐患_排查计划项目 一对多
            var yhv2 = modelBuilder.Entity<YH_ScreeningPlan>();
            yhv2.HasMany(u => u.YH_ScreeningPlanProject).WithOne(a => a.YH_ScreeningPlan).HasForeignKey(i => i.ScreeningPlanId);
            //隐患_排查计划主表 -> 参与人员 一对多
            yhv2.HasMany(a => a.YH_ScreeningPlanUser).WithOne().HasForeignKey(i => i.ScreeningPlanId);
            //隐患_排查计划主表 -> 提醒表 一对多
            yhv2.HasMany(a => a.YH_ScreeningPlanTask).WithOne().HasForeignKey(i => i.ScreeningPlanId);
            //隐患_排查计划主表 -> 负责人 一对多
            yhv2.HasOne(a => a.SysUser).WithMany().HasForeignKey(i => i.PersonInCharge);
            //排查单位
            yhv2.HasOne(a => a.SysDepartment).WithMany().HasForeignKey(i => i.Unit);

            yhv2.RemoeForeignKey("ScreeningPlanId");
            modelBuilder.Entity<YH_ScreeningPlanProject>().RemoeForeignKey("ScreeningPlanId");



            //隐患_排查计划提醒表
            var yh_plantask = modelBuilder.Entity<YH_ScreeningPlanTask>();
            //隐患_排查计划提醒表 -> 隐患_排查计划提醒人表 一对多
            yh_plantask.HasMany(a => a.YH_ScreeningPlanWarn).WithOne().HasForeignKey(a => a.ScreeningPlanTaskId);

            //隐患_排查计划提醒人表
            var yh_planwarn = modelBuilder.Entity<YH_ScreeningPlanWarn>();
            //隐患_排查计划提醒人表 -> 用户表 一对一
            yh_planwarn.HasOne(a => a.SysUser).WithMany().HasForeignKey(a => a.UserId);

            //隐患_排查计划参与人表
            var yh_planuser = modelBuilder.Entity<YH_ScreeningPlanUser>();
            //隐患_排查计划参与人表 -> 用户表 一对一
            yh_planuser.HasOne(a => a.SysUser).WithMany().HasForeignKey(a => a.UserId);

            var yhv3 = modelBuilder.Entity<YH_ScreeningPlanProject>();
            //隐患_排查计划项目->区域 一对多
            yhv3.HasOne(a => a.SysArea).WithMany().HasForeignKey(a => a.Region);
            //隐患_排查计划项目->隐患_排查计划项目明细 一对多
            yhv3.HasMany(a => a.YH_ScreeningPlanProDetail).WithOne().HasForeignKey(a => a.ScreeningPlanProjectId);

            var yh_planprodetail = modelBuilder.Entity<YH_ScreeningPlanProDetail>();
            yh_planprodetail.HasOne(a => a.YH_Grade).WithMany().HasForeignKey(a => a.RiskLevel);
            yh_planprodetail.HasOne(a => a.SysArea).WithMany().HasForeignKey(a => a.Region);
            //隐患_隐患等级通知岗位
            var yh_grade = modelBuilder.Entity<YH_Grade>();
            yh_grade.HasMany(a => a.YH_GradePost).WithOne().HasForeignKey(a => a.GradeId);

            //隐患排查
            var yh_screening = modelBuilder.Entity<YH_Screening>();
            yh_screening.HasMany(a => a.YH_ScreeningProject).WithOne().HasForeignKey(a => a.ScreeningId);
            yh_screening.HasMany(a => a.YH_ScreeningUser).WithOne().HasForeignKey(a => a.ScreeningId);
            yh_screening.HasOne(a => a.SysUser).WithMany().HasForeignKey(a => a.PersonInCharge);
            yh_screening.HasOne(a => a.YH_ScreeningPlan).WithMany().HasForeignKey(a => a.ScreeningPlanId);
            yh_screening.HasOne(a => a.SysDepartment).WithMany().HasForeignKey(a => a.Unit);

            //隐患排查项目
            var yh_screeningProject = modelBuilder.Entity<YH_ScreeningProject>();
            yh_screeningProject.HasMany(a => a.YH_ScreeningDetail).WithOne().HasForeignKey(a => a.ScreeningProjectId);
            yh_screeningProject.HasOne(a => a.SysArea).WithMany().HasForeignKey(a => a.Region);

            //隐患排查明细
            var yh_screeninguser = modelBuilder.Entity<YH_ScreeningUser>();
            //隐患排查明细参与人表 -> 用户表 一对一
            yh_screeninguser.HasOne(a => a.SysUser).WithMany().HasForeignKey(a => a.UserId);

            //隐患排查明细
            var yh_screeningDetail = modelBuilder.Entity<YH_ScreeningDetail>();

            yh_screeningDetail.HasOne(a => a.YH_Grade).WithMany().HasForeignKey(a => a.RiskLevel);
            yh_screeningDetail.HasOne(a => a.YH_StandingBook).WithOne().HasForeignKey<YH_ScreeningDetail>(a => a.StandingBookId);


            var stopBigType = modelBuilder.Entity<JG_STOPBigType>();
            //隐患_SmallType
            var stopSmallType = modelBuilder.Entity<JG_STOPSmallType>();

            //隐患_STOP观察记录
            var stopV1 = modelBuilder.Entity<JG_STOPObserveMain>();
            //隐患_STOP观察记录->  隐患_STOP观察记录明细表 一对多
            stopV1.HasMany(a => a.JG_STOPObserveDetail).WithOne().HasForeignKey(a => a.STOPObserveMain);

            //隐患_STOP观察记录 与 人员 SysUser  的关系
            stopV1.HasOne(u => u.SysUser).WithOne().HasForeignKey<JG_STOPObserveMain>(i => i.observePeople);

            //JG_StopTimes关系 by songyf
            var st = modelBuilder.Entity<JG_STOPTimes>();
            st.HasOne(a => a.SysDepartment).WithMany().HasForeignKey(c => c.department);
            st.HasOne(a => a.SysDuty).WithMany().HasForeignKey(c => c.duty);
            st.HasOne(a => a.SysUser).WithMany().HasForeignKey(c => c.name);
            #endregion

            #region  承包商
            var cv = modelBuilder.Entity<CBS_List>();
            cv.HasOne(a => a.SysDepartment).WithMany().HasForeignKey(c => c.ManageDept);
            cv.HasMany(c => c.CBS_ListAttachments).WithOne().HasForeignKey(a => a.CBS_ListId);
            modelBuilder.Entity<CBS_ListAttachment>().HasOne(c => c.SysUser).WithMany().HasForeignKey(c => c.UploadUser);

            var cbsuser = modelBuilder.Entity<CBS_User>();
            cbsuser.HasOne(a => a.CBS_List).WithMany().HasForeignKey(c => c.CBSListId);
            cbsuser.HasMany(c => c.CBS_UserAttachments).WithOne().HasForeignKey(a => a.CBSUserId);



            var cbsBreak = modelBuilder.Entity<CBS_BreakRules>();
            cbsBreak.HasOne(a => a.CBS_List).WithMany().HasForeignKey(c => c.CBSListId);
            cbsBreak.HasOne(a => a.CBS_User).WithMany().HasForeignKey(c => c.ViolatorsID);
            cbsBreak.HasOne(a => a.SysArea).WithMany().HasForeignKey(c => c.AreaID);
            cbsBreak.HasMany(u => u.CBS_BreakRulesApprove).WithOne().HasForeignKey(i => i.BreakRulesId);
            cbsBreak.HasMany(u => u.CBS_BreakRulesAttachment).WithOne().HasForeignKey(i => i.BreakRulesId);
            modelBuilder.Entity<CBS_BreakRulesAttachment>().HasOne(c => c.SysUser).WithMany().HasForeignKey(c => c.UploadUser);


            var cbsBreakRulesApproveDict = modelBuilder.Entity<CBS_BreakRulesApproveDict>();
            cbsBreakRulesApproveDict.HasOne(a => a.SysDuty).WithMany().HasForeignKey(c=>c.ExamineDuty);


            //承包商入场人员名单
            var rcrymd1 = modelBuilder.Entity<CBS_IntoFactoryUser>();
            rcrymd1.HasOne(a => a.CBS_User).WithOne().HasForeignKey<CBS_IntoFactoryUser>(i => i.CBSUserId);//承包商人员
            rcrymd1.HasOne(a => a.CBS_List).WithMany().HasForeignKey(c => c.CBSListId);//.WithOne().HasForeignKey<CBS_IntoFactoryUser>(i => i.CBSListId);//承包商

            //承包商入场工具
            var rcgj1 = modelBuilder.Entity<CBS_IntoFactoryTool>();
            rcgj1.HasMany(u => u.CBS_OutFactoryTool).WithOne().HasForeignKey(i => i.IntoFactoryToolId);
            rcgj1.HasOne(a => a.CBS_List).WithMany().HasForeignKey(c => c.CBSListId);//.HasForeignKey<CBS_IntoFactoryTool>(i => i.CBSListId);//承包商

            //日常评估检查项(日常评估模板)
            var cbsdci1 = modelBuilder.Entity<CBS_DayCheckItem>();
            cbsdci1.HasOne(a => a.SysDataDic).WithMany().HasForeignKey(c => c.AssessTypeId);//承包商类型




            //承包商_日常评估考核
            var cbsrcpgkh1 = modelBuilder.Entity<CBS_DayCheck>();
            cbsrcpgkh1.HasOne(a => a.SysUser).WithMany().HasForeignKey(c => c.AssessUserId);//评估人员
            cbsrcpgkh1.HasOne(a => a.CBS_List).WithMany().HasForeignKey(c => c.CBSListID);//承包商
            cbsrcpgkh1.HasOne(a => a.CBS_DayCheckTask).WithMany().HasForeignKey(c => c.DayCheckTaskID);//日常评估考核任务
            cbsrcpgkh1.HasOne(a => a.SysDataDic).WithMany().HasForeignKey(c => c.AssessTypeId);//评估类别
            //cbsrcpgkh1.HasOne(a => a.SysUser).WithOne().HasForeignKey<SysUser>(c => c.Id);//评估人员
            //cbsrcpgkh1.HasOne(a => a.CBS_List).WithOne().HasForeignKey<CBS_List>(c => c.Id);//承包商
            //cbsrcpgkh1.HasOne(a => a.CBS_DayCheckTask).WithOne().HasForeignKey<CBS_DayCheckTask>(c => c.Id);//日常评估考核任务
            //cbsrcpgkh1.HasOne(a => a.SysDataDic).WithOne().HasForeignKey<SysDataDic>(c => c.Id);//评估类别
            cbsrcpgkh1.HasMany(a => a.CBS_DayCheckDetail).WithOne().HasForeignKey(a => a.CBSDayCheckId);//日常评估考核明细

            var cbsrcpgkhxq1 = modelBuilder.Entity<CBS_DayCheckDetail>();
            cbsrcpgkhxq1.HasOne(a => a.CBS_DayCheckItem).WithMany().HasForeignKey(c => c.CBSDayCheckItemId);//日常评估检查项ID


            //总体评估检查项(总体评估模板)
            var cbsztjcx1 = modelBuilder.Entity<CBS_TotalCheckItem>();
            cbsztjcx1.HasOne(a => a.SysDataDic).WithMany().HasForeignKey(c => c.AssessTypeId);//承包商类型




            //承包商_总体评估考核
            var cbsztpgkh1 = modelBuilder.Entity<CBS_TotalCheck>();
            cbsztpgkh1.HasOne(a => a.SysUser).WithMany().HasForeignKey(c => c.TerritorialAppraiser);//评估人员
            cbsztpgkh1.HasOne(a => a.SysUser1).WithMany().HasForeignKey(c => c.BusinessManagementEvaluator);//业务管理评价人
            cbsztpgkh1.HasOne(a => a.SysUser2).WithMany().HasForeignKey(c => c.SummaryEvaluator);//总结评价人
            cbsztpgkh1.HasOne(a => a.CBS_List).WithMany().HasForeignKey(c => c.CBSListID);//承包商
            cbsztpgkh1.HasOne(a => a.CBS_TotalCheckTask).WithMany().HasForeignKey(c => c.TotalCheckTaskID);//总体评估考核任务
            cbsztpgkh1.HasOne(a => a.SysDataDic).WithMany().HasForeignKey(c => c.AssessTypeId);//评估类别
            cbsztpgkh1.HasMany(a => a.CBS_TotalCheckDetail).WithOne().HasForeignKey(a => a.CBSTotalCheckId);//总体评估考核明细
            cbsztpgkh1.HasMany(a => a.CBS_TotalCheckHeadApprove).WithOne().HasForeignKey(a => a.TotalCheckID);//总体评估考核领导签字

            //承包商_总体评估考核明细
            var cbsztpgkhxq1 = modelBuilder.Entity<CBS_TotalCheckDetail>();
            cbsztpgkhxq1.HasOne(a => a.CBS_TotalCheckItem).WithMany().HasForeignKey(c => c.CBSTotalCheckItemId);//总体评估检查项ID

            //承包商_承包商类别同主管部门
            var cbslbtzgbm1 = modelBuilder.Entity<CBS_CategoryManage>();
            cbslbtzgbm1.HasOne(a => a.SysDepartment).WithMany().HasForeignKey(c => c.ManageDept);//主管部门

            //承包商_承包商类别审批领导
            var cbslbspld1 = modelBuilder.Entity<CBS_CategoryApproveDuty>();
            cbslbspld1.HasOne(a => a.SysDuty).WithMany().HasForeignKey(c => c.DutyId);//领导岗位

            //承包商_总体评估考核领导签字
            var cbskhldqz1 = modelBuilder.Entity<CBS_TotalCheckHeadApprove>();
            cbskhldqz1.HasOne(a => a.SysDuty).WithMany().HasForeignKey(c => c.DutyId);//领导岗位

            //
            //培训课件
            var cbspxkj1 = modelBuilder.Entity<CBS_Course>();
            //cbspxkj1.HasOne(u => u.CBS_CourseClass).WithMany().HasForeignKey(i => i.CourseClassId);//课件分类
            cbspxkj1.HasOne(u => u.PX_CourseClass).WithMany().HasForeignKey(i => i.CourseClassId);//课件分类

            //承包商_试题
            var cbsst1 = modelBuilder.Entity<CBS_Questions>();
            cbsst1.HasOne(u => u.CBS_Course).WithMany().HasForeignKey(i => i.CBSCourseId);//课件
            cbsst1.HasMany(a => a.CBS_QuestionsOptions).WithOne().HasForeignKey(a => a.CBSQuestionsId);//承包商_试题选项


            //承包商_考试试题主表
            var cbsksst1 = modelBuilder.Entity<CBS_ExamQuestions>();
            cbsksst1.HasOne(u => u.CBS_Course).WithMany().HasForeignKey(i => i.CBSCourseId);//课件
            cbsksst1.HasMany(a => a.CBS_ExamQuestionsDetail).WithOne().HasForeignKey(a => a.CBSExamQuestionsId);//考试试题明细表
            cbsksst1.HasMany(a => a.CBS_ExamQuestionsUser).WithOne().HasForeignKey(a => a.CBSExamQuestionsId);//考试答题人

            var cbsksstmx1 = modelBuilder.Entity<CBS_ExamQuestionsDetail>();
            cbsksstmx1.HasOne(u => u.CBS_Questions).WithMany().HasForeignKey(i => i.CBSQuestionsId);//试题

            var cbsksdtr1 = modelBuilder.Entity<CBS_ExamQuestionsUser>();
            cbsksdtr1.HasOne(u => u.CBS_User).WithMany().HasForeignKey(i => i.UserId);//人员

            #endregion


            #region 风险

            //风险_操作及相关作业活动
            var fx1 = modelBuilder.Entity<FX_OperationAndWork>();
            //风险_操作及相关作业活动->区域 一对一
            fx1.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);
            //fx1.HasOne(u => u.SysArea).WithOne().HasForeignKey<FX_OperationAndWork>(i => i.FXTypeID);

            //风险_生产作业活动
            var fx2 = modelBuilder.Entity<FX_WorkActivity>();
            //风险_生产作业活动->区域 一对一
            fx2.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);


            //风险_操作程序
            var fx3 = modelBuilder.Entity<FX_Operation>();
            //风险_操作程序->区域 一对一
            fx3.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);

            //检维修作业活动
            var fx4 = modelBuilder.Entity<FX_CheckRepair>();
            //检维修作业活动->区域 一对一
            fx4.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);

            //检维修作业活动工具
            var fx5 = modelBuilder.Entity<FX_CheckRepairTool>();
            //检维修作业活动工具->区域 一对一
            fx5.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);


            //风险_设备设施
            var fx6 = modelBuilder.Entity<FX_Equipment>();
            //风险_设备设施->区域 一对一
            fx6.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);

            //风险_工艺介质
            var fx7 = modelBuilder.Entity<FX_Process>();
            //风险_工艺介质->区域 一对一
            fx7.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);

            //风险_场所环境
            var fx8 = modelBuilder.Entity<FX_Environment>();
            //风险_场所环境->区域 一对一
            fx8.HasOne(u => u.SysArea).WithMany().HasForeignKey(i => i.AreaID);


            #endregion

            var sysArea = modelBuilder.Entity<SysArea>();
            sysArea.HasMany(u => u.SysAreaPoint).WithOne().HasForeignKey(i => i.SysAreaId);
            sysArea.HasMany(u => u.SysJobPlace).WithOne().HasForeignKey(i => i.SysAreaID);

            //启用Guid主键类型扩展
            v2.HasMany(u => u.SysUserDuties).WithOne().HasForeignKey(i => i.SysUserId);
            modelBuilder.HasPostgresExtension("uuid-ossp");



            #region 机构与职责

            var jgps1 = modelBuilder.Entity<JG_PersonalSafety>();

            //个人安全行动计划->个人安全行动计划明细 一对多
            jgps1.HasMany(u => u.JG_PersonalSafetyDetail).WithOne().HasForeignKey(i => i.PersonalSafetyId);
            //个人安全行动计划_JG_PersonalSafety 与 人员 SysUser  的关系
            jgps1.HasOne(u => u.SysUser).WithOne().HasForeignKey<JG_PersonalSafety>(i => i.CreateUser);
            //个人安全行动计划_JG_PersonalSafety 与 部门 SysDepartment 的关系

            jgps1.HasOne(u => u.SysDepartment).WithOne().HasForeignKey<JG_PersonalSafety>(i => i.CreateDept);

            var jgP1 = modelBuilder.Entity<JG_HSEPlan>();
            //JG_HSEPlan 与 人员 SysUser  的关系
            jgP1.HasOne(u => u.SysUser).WithOne().HasForeignKey<JG_HSEPlan>(i => i.Personliable);


            var jgjat1 = modelBuilder.Entity<JG_JobsAssessTemplate>();

            //岗位安全考核模板->岗位安全考核模板明细 一对多
            jgjat1.HasMany(a => a.JG_JobsAssessTemplateDetail).WithOne().HasForeignKey(a => a.JobsAssessTemplateID);
            jgjat1.HasOne(a => a.SysDuty).WithOne().HasForeignKey<JG_JobsAssessTemplate>(i => i.postID);
            jgjat1.HasOne(a => a.SysDepartment).WithOne().HasForeignKey<JG_JobsAssessTemplate>(i => i.postID2);


            //岗位安全职责考核->岗位安全职责考核明细 一对多
            var jgja1 = modelBuilder.Entity<JG_JobsAssess>();
            jgja1.HasMany(a => a.JG_JobsAssessDetail).WithOne().HasForeignKey(a => a.JGJobsAssessID);
            jgja1.HasOne(a => a.SysUser).WithOne().HasForeignKey<JG_JobsAssess>(i => i.postPerson);
            //jgja1.HasOne(a => a.SysDuty).WithOne().HasForeignKey<JG_JobsAssess>(i => i.PostNameID);
            jgja1.HasOne(a => a.JG_JobsAssessTask).WithOne().HasForeignKey<JG_JobsAssess>(i => i.JobsAssessTask);
            //jgja1.HasOne(a => a.SysDepartment).WithOne().HasForeignKey<JG_JobsAssess>(i => i.PostID);
            jgja1.HasOne(a => a.superiorLeadersInfo).WithOne().HasForeignKey<JG_JobsAssess>(i => i.superiorLeaders);

            var jgjatask1 = modelBuilder.Entity<JG_JobsAssessTask>();
            jgjatask1.HasOne(a => a.SysDuty).WithOne().HasForeignKey<JG_JobsAssessTask>(i => i.PostNameID);
            jgjatask1.HasOne(a => a.SysDepartment).WithOne().HasForeignKey<JG_JobsAssessTask>(i => i.PostID);


            //履职能力评估计划->履职能力评估计划被评估人 一对多
            var jgdp1 = modelBuilder.Entity<JG_DutiesPlan>();
            jgdp1.HasMany(x => x.JG_DutiesPlanAssessedPerson).WithOne().HasForeignKey(x => x.DutiesPlanID);
            #endregion

            #region  总部
            #region 总部_安全信息分享
            var zbss = modelBuilder.Entity<ZB_SafetyShare>();
            zbss.HasOne(u => u.SysUser).WithOne().HasForeignKey<ZB_SafetyShare>(i => i.CreateUser);
            #endregion

            #region 总部_课件
            //课件->课件试题 一对多
            var zbkj1 = modelBuilder.Entity<ZB_Course>();
            zbkj1.HasMany(u => u.ZB_CourseQuestions).WithOne().HasForeignKey(i => i.ZBCourseId);

            //课件试题->课件试题选项 一对多
            var zbkjst1 = modelBuilder.Entity<ZB_CourseQuestions>();
            zbkjst1.HasMany(u => u.ZB_QuestionsOptions).WithOne().HasForeignKey(i => i.ZBCourseQuestionsId);

            #endregion

            #region 总部_年度培训计划

            //培训计划->培训计划考题 一对多
            var zbpxjh1 = modelBuilder.Entity<ZB_TrainPlan>();
            zbpxjh1.HasMany(u => u.ZB_TrainPlanQuestions).WithOne().HasForeignKey(i => i.ZBTrainPlanId);

            //培训计划->培训计划课题
            zbpxjh1.HasOne(c => c.ZB_Course).WithMany().HasForeignKey(i => i.ZBCourseId);

            #endregion

            #region 总部_在线考试
            //培训计划->考试人员 
            var zb_exam_person = modelBuilder.Entity<ZB_TrainPlan>();
            zb_exam_person.HasMany(u => u.ZB_ExamUser).WithOne().HasForeignKey(a => a.ZBTrainPlanId);

            //总部_考试人员->总部_考试记录
            var zb_examperson_record = modelBuilder.Entity<ZB_ExamUser>();
            zb_examperson_record.HasMany(u => u.ZB_ExamRecord).WithOne().HasForeignKey(a => a.PersonID);

            //总部_考试记录->总部_考试试题
            var zb_exampersonrecord_question = modelBuilder.Entity<ZB_ExamRecord>();
            zb_exampersonrecord_question.HasMany(u => u.ZB_ExamQuestions).WithOne().HasForeignKey(a => a.ZBExamRecordID);


            #endregion



            #region 总部_安委会会议纪要
            //总部_安委会会议纪要_ZB_SafetyMeeting与SysUser关系
            var fxawh = modelBuilder.Entity<ZB_SafetyMeeting>();
            fxawh.HasOne(u => u.SysUser).WithOne().HasForeignKey<ZB_SafetyMeeting>(i => i.CreateUser);
            #endregion

            #region 总部_安全里程碑活动--附件表
            //总部_安全里程碑活动_ZB_SafetyMilestoneFile与SysUser关系
            var fxawhd = modelBuilder.Entity<ZB_SafetyMilestoneFile>();
            fxawhd.HasOne(u => u.SysUser).WithOne().HasForeignKey<ZB_SafetyMilestoneFile>(i => i.CreateUser);
            #endregion

            #region 总部_安全分委会成员
            //总部_安全分委会成员_ZB_SafetyUser与SysDepartment关系
            var ZbDep = modelBuilder.Entity<ZB_SafetyUser>();
            ZbDep.HasOne(u => u.SysDepartment).WithOne().HasForeignKey<ZB_SafetyUser>(i => i.Department);
            #endregion

            #region 总部_安全分委会成员_ZB_SafetyUser与总部_安全分委会架构ZB_SafetyFrameWork关系
            var ZbFra = modelBuilder.Entity<ZB_SafetyUser>();
            ZbFra.HasOne(u => u.ZB_SafetyFrameWork).WithOne().HasForeignKey<ZB_SafetyUser>(i => i.ZBSafetyFrameWorkId);
            #endregion


            # region 总部_安全分委会成员_ZB_SafetyUser与SysUser关系
            var ZbUser = modelBuilder.Entity<ZB_SafetyUser>();
            ZbUser.HasOne(u => u.SysUser).WithOne().HasForeignKey<ZB_SafetyUser>(i => i.Name);
            #endregion

            # region 总部_安全分委会成员_ZB_SafetyUser与办公安全ZB_OfficeSafety关系
            var ZbUser1 = modelBuilder.Entity<ZB_SafetyUser>();
            ZbUser1.HasMany(u => u.ZB_OfficeSafety).WithOne().HasForeignKey(i => i.Name);
            // ZbUser1.HasMany(u => u.ZB_OfficeSafety).
            #endregion

           # region 总部_办公安全_ZB_OfficeSafety与SysUser关系
            var ZbBan = modelBuilder.Entity<ZB_OfficeSafety>();
            ZbBan.HasOne(u => u.SysUser).WithOne().HasForeignKey<ZB_OfficeSafety>(i => i.Name);
            #endregion

           # region 总部_办公安全_ZB_OfficeSafety与SysUser关系
            var ZbBan1 = modelBuilder.Entity<ZB_OfficeSafety>();
            ZbBan1.HasOne(u => u.ZB_ProjectTypes).WithOne().HasForeignKey<ZB_OfficeSafety>(i => i.itemType);
            #endregion


            #endregion

            #region

            //总部_安全管理培训师
            var zbaqglpxs1 = modelBuilder.Entity<ZB_SafetyTrainer>();

            zbaqglpxs1.HasOne(a => a.SysDataDic).WithMany().HasForeignKey(c => c.type);//管理师类型
            zbaqglpxs1.HasMany(a => a.ZB_SafetyTrainerDetail).WithOne().HasForeignKey(a => a.ZBSafetyTrainerId);//总部_安全管理培训师明细

            ////总部_安全管理培训师明细
            //var zbaqglpxsmx1 = modelBuilder.Entity<ZB_SafetyTrainerDetail>();
            //zbaqglpxsmx1.HasOne(a => a.ZB_SafetyTrainer).WithMany().HasForeignKey(c => c.ZBSafetyTrainerId);//管理师主表外键
            #endregion



            #region 作业

            //基础_作业表格审批流程设置
            var zy = modelBuilder.Entity<ZY_TableApproveProcess>();
            zy.Property(a => a.HolidayType).HasConversion(new EnumToNumberConverter<HolidayType, int>());
            zy.Property(a => a.ApproveType).HasConversion(new EnumToNumberConverter<ApproveType, int>());
            zy.Property(a => a.Shift).HasConversion(new EnumToNumberConverter<Shift, int>());
            zy.Property(a => a.TicketType).HasConversion(new EnumToStringConverter<TicketTypeEnum>());
            ///作业_作业内容
            var content = modelBuilder.Entity<ZY_Content>();
            content.Property(a => a.TableType).HasConversion(new EnumToStringConverter<TicketTypeEnum>());

            //var zy_number = modelBuilder.Entity<ZY_Number>();
            //zy_number.Property(a => a.TableType).HasConversion(new EnumToStringConverter<TicketTypeEnum>());

            var checkSignature = modelBuilder.Entity<ZY_CheckSignature>();
            checkSignature.Property(a => a.TableType).HasConversion(new EnumToStringConverter<TicketTypeEnum>());
            #endregion

        }

        #region  请勿修改
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.EnableSensitiveDataLogging();
        }

        public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
        {
            ChangeTracker.DetectChanges();

            var currentUserId = _httpContextAccessor.CurrentUserId();
            foreach (var entity in ChangeTracker.Entries())
            {
                if (!entity.Entity.GetType().IsSubclassOf(typeof(Entity)))
                    continue;
                var entityObj = ((Entity)entity.Entity);
                switch (entity.State)
                {
                    case EntityState.Modified:
                        {
                            entityObj.UpdateTime = DateTime.Now;
                            entityObj.UpdateUser = currentUserId;
                            break;
                        }
                    case EntityState.Added:
                        {
                            entityObj.CreateTime = DateTime.Now;
                            entityObj.CreateUser = currentUserId;
                            entityObj.UpdateTime = entityObj.CreateTime;
                            entityObj.UpdateUser = entityObj.CreateUser;
                        }
                        break;
                }

            }
            var result = await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
            return result;
        }
        #endregion
    }


}



